/**
 * ISSG Sony China 2010 
 *
 */
package cn.com.dragonsoftbravo.utils;

import java.io.InputStream;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * The Class SystemConfigHolder.
 */
public class SystemConfigHolder {

	/**
	 * The Enum SystemLeve.
	 */
	public static enum SystemLeve {

		PSSDEV, PSSUAT, PSSPRD
	}

	/**
	 * 
	 * @return
	 */
	public static boolean isWindowsOS() {

		String os = System.getProperty("os.name");
		// System.out.println(os);

		return null != os && os.startsWith("Windows");
	}

	/**
	 * 
	 * @return
	 */
	public static String getCurrentOS() {

		return System.getProperty("os.name");
	}

	/**
	 * 
	 */
	private static Log log = LogFactory.getLog(SystemConfigHolder.class);

	/**
	 * 
	 * @copyright Promoters Sell-thru System, ISSG, Sony China
	 * @author William
	 * @Revision
	 * @date 2010-4-14
	 */
	private static class MyHolder {

		private static final SystemConfigHolder instance = new SystemConfigHolder();
	}

	/**
	 * 
	 */
	private Properties properties = new Properties();

	/**
	 * Instance.
	 * 
	 * @return the system config holder
	 */
	public static SystemConfigHolder instance() {

		return MyHolder.instance;
	}

	private SystemConfigHolder() {

		load();
	}

	private void load() {
		InputStream is = null;

		try {

			is = SystemConfigHolder.class.getClassLoader().getResourceAsStream("system-base.properties");
			properties.load(is);
			is.close();
			
			if(getValue("prd").equals("1")){
				is = SystemConfigHolder.class.getClassLoader().getResourceAsStream("system-base-prd.properties");
				properties.load(is);
				is.close();
			}

		} catch (Exception e) {
			if (log.isErrorEnabled()) {
				log.error(e.toString());
			}
		} finally {
			IOCloseUtils.close(is);
		}
	}

	/**
	 * Gets the value.
	 * 
	 * @param key
	 *            the key
	 * @return the value
	 */
	public String getValue(String key) {
		
		

		return properties.getProperty(key);
	}

	/**
	 * Gets the long.
	 * 
	 * @param key
	 *            the key
	 * @return the long
	 */
	public Long getLong(String key) {

		return Long.valueOf(properties.getProperty(key));

	}

	/**
	 * Gets the interger.
	 * 
	 * @param key
	 *            the key
	 * @return the interger
	 */
	public Integer getInterger(String key) {

		return Integer.valueOf(properties.getProperty(key));
	}

	/**
	 * Gets the boolean.
	 * 
	 * @param key
	 *            the key
	 * @return the boolean
	 */
	public boolean getBoolean(String key) {

		if (null == properties.get(key)) {
			return true;
		}

		return Boolean.valueOf(properties.getProperty(key));
	}

	/**
	 * 
	 * @param key
	 * @param defval
	 * @return
	 */
	public boolean getBoolean(String key, boolean defval) {

		if (null == properties.get(key)) {
			return defval;
		}

		return Boolean.valueOf(properties.getProperty(key));

	}

	/**
	 * Gets the float.
	 * 
	 * @param key
	 *            the key
	 * @return the float
	 */
	public Float getFloat(String key) {

		return Float.valueOf(properties.getProperty(key));
	}


	/**
	 * Gets the values.
	 * 
	 * @param key
	 *            the key
	 * @return the values
	 */
	public String[] getValues(String key) {

		return properties.getProperty(key).split(",");
	}

	/**
	 * The Enum ReleaseType.
	 */
	public static enum ReleaseType {

		External, Internal
	}
	
	
	public static final String SYSTEM_MACHINE_LIST="System.Machine.List";

	/**
	 * Gets the release type.
	 * 
	 * @return the release type
	 */
	public ReleaseType getReleaseType() {

		String ts = getValue("RELEASE_TYPE");
		if (ts.equalsIgnoreCase("WAP")) {
			return ReleaseType.External;
		} else {
			return ReleaseType.Internal;
		}
	}


	/**
	 * Reload properties.
	 */
	public void reloadProperties() {
		this.properties.clear();
		this.load();
	}

	/**
	 * Gets the Long values.
	 * 
	 * @param key
	 *            the key
	 * @return the values
	 */
	public Long[] getValuesOfLong(String key) {
		String[] strValues = this.getValues(key);
		if (strValues != null && strValues.length > 0) {
			Long[] longValues = new Long[strValues.length];
			for (int i = 0; i < strValues.length; i++) {
				longValues[i] = Long.valueOf(strValues[i]);
			}
			return longValues;
		} else {
			return new Long[0];
		}

	}
}
